﻿using Summer.Windows.Context;
using Summer.Core.Ioc;

#region License
/*
 *  Copyright (c) Dumuzi.cn 2009 , All rights reserved.
 *  CLR Version: 2.0.50727.4927
 *  NameSpace: Summer.Windows.Support
 *  Author:Edwin Tai,戴佳顺
 *  Msn:edwin19861218@hotmail.com
 *  QQ: 1961218
 *  Date:2009-11-10 00:42:04
 *  Licensed under the Microsoft Public License (Ms-PL)
 *  This license governs use of the accompanying software. 
 *  If you use the software, you accept this license.
 *  If you do not accept the license, do not use the software.
 */
#endregion

namespace Summer.Windows.Support
{
    /// The class is only used in windows application
    /// In web, the method is from  Summer.Web.Support.ApplicationStateSupport
    public static class ApplicationStateSupport
    {
        /// <summary>
        /// Load container in windows
        /// </summary>
        /// <param name="m_contextxml"></param>
        /// <param name="configuration"></param>
        public static void LoadContainer(string m_contextxml, IocConfiguration configuration)
        {
            //Create new contextContainer
            WindowsContextContainer baseContainer = (WindowsContextContainer)configuration.GetRootContainer(m_contextxml);
            AbstractContextContainer<IContextObject>[] containers = configuration.GetContainers(m_contextxml);
            foreach (AbstractContextContainer<IContextObject> obj in containers)
            {
                configuration.SetContext(obj, m_contextxml, baseContainer);
            }
            //Load the baseContainer
            baseContainer.Load(null);
            //Add the base container into boarding container
            ApplicationState.BaseContextContainer = baseContainer;
        }
        /// <summary>
        /// Get root container in windows
        /// </summary>
        /// <returns></returns>
        public static WindowsContextContainer GetContainer()
        {
            return ApplicationState.BaseContextContainer;
        }
        /// <summary>
        /// Get the default context which contains contextObject
        /// </summary>
        /// <param name="application"></param>
        /// <returns></returns>
        public static Summer.Core.Ioc.Context GetContextContainer()
        {
            return (Summer.Core.Ioc.Context)GetContainer()[IocConst.DEFAULT_CONTEXT];
        }
        /// <summary>
        /// Unload container in windows
        /// </summary>
        public static void UnloadContainer()
        {
            WindowsContextContainer baseContainer = GetContainer();
            if (baseContainer != null)
            {
                baseContainer.UnLoad();
            }
        }
        /// <summary>
        /// Get the default context which contains contextObject
        /// </summary>
        /// <param name="application"></param>
        /// <returns></returns>
        public static Summer.Core.Ioc.Context GetDefaultContextContainer()
        {
            return (Summer.Core.Ioc.Context)GetContainer()[IocConst.DEFAULT_CONTEXT];
        }
        /// <summary>
        /// Get Context by name
        /// </summary>
        /// <param name="application"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Summer.Core.Ioc.Context GetContextContainerByName(string name)
        {
            return (Summer.Core.Ioc.Context)GetContainer()[name];
        }

        /// <summary>
        /// Get the config context which contains contextObject
        /// </summary>
        /// <param name="application"></param>
        /// <returns></returns>
        public static Summer.Core.Ioc.Context GetConfigContextContainer()
        {
            return (Summer.Core.Ioc.Context)GetContainer()[IocConst.CONFIG_COTEXT];
        }
    }
}
